package com.shtel.bss.saas.tenant.service.impl;

import com.github.pagehelper.PageInfo;
import com.shtel.bss.saas.core.data.common.service.DataServiceImpl;
import com.shtel.bss.saas.tenant.bo.SysGlobalAuthBo;
import com.shtel.bss.saas.tenant.bo.QrySysGlobalAuthResBo;
import com.shtel.bss.saas.tenant.bo.SysTenantBo;
import com.shtel.bss.saas.tenant.entity.SysGlobalAuth;
import com.shtel.bss.saas.tenant.entity.SysTenantAuthJoin;
import com.shtel.bss.saas.tenant.manager.SysGlobalAuthManager;
import com.shtel.bss.saas.tenant.manager.SysTenantAuthJoinManager;
import com.shtel.bss.saas.tenant.manager.SysTenantManager;
import com.shtel.bss.saas.tenant.mapstrut.EntityDtoMapper;
import com.shtel.bss.saas.tenant.service.ISysGlobalAuthService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;


/**
 * 基础权限表服务
 * @author xiezhiwei 2020-02-05 22:02
 */
@Service("SysGlobalAuthService")
public class SysGlobalAuthServiceImpl extends DataServiceImpl implements ISysGlobalAuthService {

    @Override
    public PageInfo<QrySysGlobalAuthResBo> querySysGlobalAuthList(String authName, String authCode, int pageSize, int pageNum) {
        SysGlobalAuth sysGlobalAuth = new SysGlobalAuth();
        //不按条件查询，查出所有的权限 返回树形结构
        if(StringUtils.isEmpty(authName) && StringUtils.isEmpty(authCode)){
            //sysGlobalAuth.setParentId(0L);
            PageInfo<SysGlobalAuth> sysGlobalAuthPageInfo =
                    SysGlobalAuthManager.selectEntity(sysGlobalAuth,pageSize,pageNum);
            PageInfo<QrySysGlobalAuthResBo> sysGlobalAuthList = EntityDtoMapper.INSTANCE.
                    toQrySysGlobalAuthResBoPageInfo(sysGlobalAuthPageInfo);
            for(QrySysGlobalAuthResBo sysGlobalAuthResBo : sysGlobalAuthList.getList()){
                List<QrySysGlobalAuthResBo> childSysAuths =  this.querySysGlobalAuths(sysGlobalAuthResBo.getAuthId());
                sysGlobalAuthResBo.setChildrenAuths(childSysAuths);
            }
            return sysGlobalAuthList;
        }
        //按照权限名称或者权限编码查询 结果不做处理，不返回树形结构，
        sysGlobalAuth.setAuthName(authName);
        sysGlobalAuth.setAuthCode(authCode);
        return EntityDtoMapper.INSTANCE.
            toQrySysGlobalAuthResBoPageInfo(SysGlobalAuthManager.selectEntity(sysGlobalAuth,pageSize,pageNum));
    }

    @Override
    public void saveSysGlobalAuth(SysGlobalAuthBo sysGlobalAuthBo) {
        SysGlobalAuthManager.saveEntity(EntityDtoMapper.INSTANCE.toSysGlobalAuth(sysGlobalAuthBo));
    }

    @Override
    public void updateSysGlobalAuth(SysGlobalAuthBo sysGlobalAuthBo) {
        SysGlobalAuthManager.updateEntity(EntityDtoMapper.INSTANCE.toSysGlobalAuth(sysGlobalAuthBo));
    }

    @Override
    @Transactional
    public void removeSysGlobalAuth(List<Long> authIds) {
       if(authIds != null&& !authIds.isEmpty()){
           for (Long authId : authIds) {
               SysGlobalAuth sysGlobalAuth = new  SysGlobalAuth();
               sysGlobalAuth.setAuthId(authId);
               sysGlobalAuth.setIsDelete((byte) 1);
               SysGlobalAuthManager.updateEntity(sysGlobalAuth);
           }
       }
    }

    @Override
    @Transactional
    public void updateAuthTenant(Long authId,  List<Long> tenantIds) {
        for (Long tenantId : tenantIds) {
            SysTenantAuthJoin sysTenantAuthJoin = new SysTenantAuthJoin();
            sysTenantAuthJoin.setAuthId(authId);
            sysTenantAuthJoin.setTenantId(tenantId);
            sysTenantAuthJoin.setCreateTime(new Date());
            SysTenantAuthJoinManager.saveEntity(sysTenantAuthJoin);
        }
    }

    @Override
    public List<QrySysGlobalAuthResBo> querySysGlobalAuthList(Byte authType) {
        SysGlobalAuth sysGlobalAuth = new SysGlobalAuth();
        sysGlobalAuth.setAuthType(authType);
        List<SysGlobalAuth> sysGlobalAuthList =  SysGlobalAuthManager.selectEntity(sysGlobalAuth);
        if(CollectionUtils.isEmpty(sysGlobalAuthList)){
            return new ArrayList<>();
        }
        List<QrySysGlobalAuthResBo> sysGlobalAuthResBoList =  EntityDtoMapper.INSTANCE.
                toQrySysGlobalAuthResBoList(sysGlobalAuthList);
        Map<Long, List<QrySysGlobalAuthResBo>> sysGlobalAuthMap = new HashMap<>();
        List<QrySysGlobalAuthResBo> rootSysGlobalAuth = new ArrayList<>();
        for(QrySysGlobalAuthResBo sysGlobalAuthResBo : sysGlobalAuthResBoList){
            List<QrySysGlobalAuthResBo> childSysGlobalAuth;
            if((childSysGlobalAuth = sysGlobalAuthMap.get(sysGlobalAuthResBo.getAuthId())) == null){
                childSysGlobalAuth = new ArrayList<>();
                sysGlobalAuthMap.put(sysGlobalAuthResBo.getAuthId(),childSysGlobalAuth);
            }
            sysGlobalAuthResBo.setChildrenAuths(childSysGlobalAuth);
            if(sysGlobalAuthResBo.getParentId() == 0){
                rootSysGlobalAuth.add(sysGlobalAuthResBo);
                continue;
            }
            List<QrySysGlobalAuthResBo> botherSysGlobalAuth;
            if((botherSysGlobalAuth = sysGlobalAuthMap.get(sysGlobalAuthResBo.getParentId())) == null){
                botherSysGlobalAuth = new ArrayList<>();
                sysGlobalAuthMap.put(sysGlobalAuthResBo.getParentId(),botherSysGlobalAuth);
            }
            botherSysGlobalAuth.add(sysGlobalAuthResBo);

        }
        return rootSysGlobalAuth;
    }

    @Override
    public List<QrySysGlobalAuthResBo> querySysGlobalAuthList() {
        return this.querySysGlobalAuthList(null);
    }

    @Override
    public PageInfo<SysTenantBo> querySysTenantnotGrantedAuthId(Long authId,int pageSize,int pageNum) {
        return EntityDtoMapper.INSTANCE.toSysTenantBoPageInfo(SysTenantManager.selectEntityByAuthIdNot(authId,pageSize,pageNum));
    }

    @Override
    public PageInfo<SysTenantBo> querySysTenantGrantAuthId(Long authId,int pageSize,int pageNum) {
        return EntityDtoMapper.INSTANCE.toSysTenantBoPageInfo(SysTenantManager.selectEntityByAuthId(authId,pageSize,pageNum));
    }

    private List<QrySysGlobalAuthResBo> querySysGlobalAuths(Long parentId){
        if(parentId == null){
            return new ArrayList<>();
        }
        SysGlobalAuth sysGlobalAuth = new SysGlobalAuth();
        sysGlobalAuth.setParentId(parentId);
        List<QrySysGlobalAuthResBo> sysGlobalAuthList = EntityDtoMapper.INSTANCE.
                toQrySysGlobalAuthResBoList(SysGlobalAuthManager.selectEntity(sysGlobalAuth));
        for(QrySysGlobalAuthResBo sysGlobalAuth1 : sysGlobalAuthList ){
            sysGlobalAuth1.setChildrenAuths(this.querySysGlobalAuths(sysGlobalAuth1.getAuthId()));
        }
        return sysGlobalAuthList;
    }


}